<?php
namespace app\admin\model;
use app\common\model\CommonModel;
use app\common\model\OperationLog;
use app\common\model\Access;
use app\common\model\Admin;
use app\common\model\Shop;
use app\common\model\Group;
use think\Session;
use think\Db;

class AdminModel extends CommonModel
{
	/**
	* 添加账号
	*/
	public static function serAdmin($info)
	{
		/*判断是否存在账号*/
		$Admin = new Admin();
		if($Admin->adminField(['account'=>$info['account'],'shop_id'=>$info['shop_id']],'account'))
		{
			return back(config('state.STATUS_CODE_FIVE'),config('state.STATUS_MSG_FIVE'));
		}
		if(!array_key_exists('role',$info))
		{
			return back(config('state.STATUS_CODE_THREE'),config('state.STATUS_MSG_THREE'));
		}
		/*开启事物*/
		 Db::startTrans();
		 $data['account'] = $info['account'];
		 $data['password'] = get_pwd('123456',config('overall.PWD_STR'));
		 $data['shop_id'] = $info['shop_id'];
		 $data['admin_type'] = $info['admin_type'];
		 $data['nkname'] = $info['nkname'];
         $data['phone'] = $info['phone'];
		 $data['remarks'] = $info['remarks'];
		 $data['addtime'] = time();
		 if(!$admin_id = $Admin->serAdmin($data))
		 {
			 Db::rollback();
			 return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		 }
		 $dataarr = [];
		 foreach($info['role'] as $v)
		 {
			 $dataarr[] = ['agid'=>$v,'admin_id'=>$admin_id];
		 }
		 /*关联*/
		 if(!(new Access())->serAccess($dataarr))
		 {
			 Db::rollback();
			 return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		 }
		 /*日志信息*/
		 $log['admin_id'] = 1;
		 $log['table_name'] = 'admin,access';
		 $log['id'] = $admin_id;
		 $log['func_name'] = 'addadmin';
		 $log['event'] = config('overall.EVENT')[0];
		 $log['content'] = '添加管理员账号';
		 if(!(new OperationLog())->serOperationLog($log))
		 {
			 Db::rollback();
			 return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		 }
		 Db::commit();
		 return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 修改账号
	*/
	public static function updateAdmin($info)
	{
		if(!array_key_exists('role',$info))
		{
			return back(config('state.STATUS_CODE_THREE'),config('state.STATUS_MSG_THREE'));
		}
		/*开启事物*/
		 Db::startTrans();
		 $data['account'] = $info['account'];
		 $data['shop_id'] = $info['shop_id'];
		 $data['admin_type'] = $info['admin_type'];
		 $data['nkname'] = $info['nkname'];
         $data['phone'] = $info['phone'];
		 $data['remarks'] = $info['remarks'];
		 if((new Admin())->updateAdmin(['admin_id'=>$info['aid']],$data) === false)
		 {
			 Db::rollback();
			 return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		 }
		 /*角色*/
		 $tianjia = [];
		 $shanchu = [];
		 $Access = new Access();
		 $agidarr = $Access->getAccessArr(['admin_id'=>$info['aid']],'agid');
		 $intersection = array_intersect($agidarr, $info['role']);
		 if($intersection)
		 {
			$tianjia = array_diff($info['role'],$intersection);
			$shanchu = array_diff($agidarr,$intersection);
		 }else{
			 $tianjia = $info['role'];
			 $shanchu = $agidarr;
		 }
		 /*添加*/
		 if($tianjia)
		 {
			 $dataarr = [];
			 foreach($tianjia as $v)
			 {
				 $dataarr[] = ['agid'=>$v,'admin_id'=>$info['aid']];
			 }
			if(!$Access->serAccess($dataarr))
			 {
				 Db::rollback();
				 return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
			 } 
		 }
		 /*删除*/
		 if($shanchu)
		 {
			 if($Access->delAccess($info['aid'],$shanchu) === false)
			 {
				 Db::rollback();
				 return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
			 }
		 }
		 /*日志信息*/
		 $log['admin_id'] = 1;
		 $log['table_name'] = 'admin,access';
		 $log['id'] = $info['aid'];
		 $log['func_name'] = 'editadmin';
		 $log['event'] = config('overall.EVENT')[2];
		 $log['content'] = '修改管理员账号';
		 if(!(new OperationLog())->serOperationLog($log))
		 {
			 Db::rollback();
			 return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		 }
		 Db::commit();
		 return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 删除
	*/
	public static function delAdmin($id)
	{
		if((new Admin())->delAdmin(['admin_id'=>$id]) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 账号列表
	*/
	public static function getAdminList($limit,$page)
	{
		$Shop = new Shop();
		$OperationLog = new OperationLog();
		$Admin = new Admin();
		switch(Session::get('admin.admin_type'))
		{
			case 0:$where['admin_type'] = ['in',[0,1]];break;
			case 1:$where['admin_type'] = ['in',[2,3]];break;
			case 2:$where['admin_type'] = ['in',[2,3]];break;
			case 3:$where['admin_type'] = ['in',[2,3]];break;
		}
		$adminList = $Admin->getAdminList($where,$limit,$page);
		foreach($adminList['data'] as $i=>$admin)
		{
			$adminList['data'][$i]['admin_type'] = self::zhiwei($admin['admin_type']);
			$adminList['data'][$i]['shop_id'] = $admin['shop_id'] == CommonModel::NUMBER_ZERO?'总部':$Shop->shopField(['shop_id'=>$admin['shop_id']],'shop_name');
			$adminid = $OperationLog->operationField(['id'=>$admin['admin_id'],'func_name'=>'addadmin','event'=>config('overall.EVENT')[0]],'admin_id');
			$adminList['data'][$i]['nkname'] = $Admin->adminField(['admin_id'=>$adminid],'nkname');
		}
		return $adminList;
	}
	public static function group($id,$admin_type)
	{
		$agidarr = (new Access())->getAccessArr(['admin_id'=>$id],'agid');
		$group = (new Group())->groupList(['admin_type'=>$admin_type]);
		foreach($group as $i=>$v)
		{
			if(in_array($v['agid'],$agidarr))
			{
				$group[$i]['checked'] = 'true';
			}else{
				$group[$i]['checked'] = 'false';
			}
		}
		return $group;
	}
	/**
	* 职位
	*/
	private static function zhiwei($admin_type)
	{
		switch($admin_type)
		{
			case CommonModel::NUMBER_ZERO:return '总部';break;
			case CommonModel::NUMBER_ONE:return '店铺营业员';break;
			case CommonModel::NUMBER_TWO:return '店铺维修技术员';break;
		}
	}
}
